package com.my.rabbitmq.five;

import com.my.utils.RabbitMQUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author 林允
 * @version 1.0
 * @description: TODO
 * @date 2023/11/29 16:01
 */

// 消息接收
public class ReceiveLogs02 {

    public static final String EXCHANGE_NAME = "logs";

    public static void main(String[] args) throws Exception {
        // 通过连接获取信道
        Channel channel = RabbitMQUtils.getChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        // 声明队列
        String queue = channel.queueDeclare().getQueue();
        // 将队列和交换机进行绑定
        channel.queueBind(queue, EXCHANGE_NAME, "");
        System.out.println("ReceiveLogs02 等待接收消息，把接收到的消息打印在屏幕上....");
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("接收到消息:" + new String(message.getBody(), "UTF-8"));
        };

        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("接收失败" + consumerTag);
        };
        channel.basicConsume(queue, true, deliverCallback, cancelCallback);;

    }

}
